<template>
	<div>
		<el-table :data="students" style="width: 100%">
			<el-table-column prop="id" label="学号" width="180">
			</el-table-column>
			<el-table-column prop="name" label="姓名" width="180">
			</el-table-column>
			<el-table-column prop="address" label="地址">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button type="warning" @click="doUpdate(scope.row)" >编辑</el-button>
					<el-button type="danger" size="small">删除</el-button>
				</template>

			</el-table-column>
		</el-table>
		<el-dialog title="修改学生信息" :visible.sync="dialogFormVisible">
			<el-form label-width="80px">
				<el-form-item label="姓名" label-width="100px">
					<el-input  v-model="updateStu.name"></el-input>
				</el-form-item>
				<el-form-item label="地址" v-model="updateStu.address" label-width="100px">
					<el-input  v-model="updateStu.address"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancelUpdate()">取 消</el-button>
				<el-button type="primary" @click="update()">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {

				dialogFormVisible: false, //控制对话框隐藏和显示
				students: [{
					id: '1001',
					name: '王小虎',
					address: '上海市普陀区金沙江路 1518 弄'
				}, {
					id: '1002',
					name: '孙悟空',
					address: '上海市普陀区金沙江路 1517 弄'
				}, {
					id: '1003',
					name: '猪八戒',
					address: '上海市普陀区金沙江路 1519 弄'
				}, {
					id: '1004',
					name: '白骨精',
					address: '上海市普陀区金沙江路 1516 弄'
				}],
				//修改时回显数据
				updateStu: {
					id: '',
					name: '',
					address: ''
				}
			}
		},
		methods: {
			doUpdate(row) {
				this.dialogFormVisible = true
				console.log(row.address);
				console.log(row.id);
			 this.students.map((item) => {
				 
					if (item.id == row.id) {
						console.log(item);
						this.updateStu.id = row.id;
						this.updateStu.name = row.name
						this.updateStu.address = row.address
					}
				})
				
			},
			update(){
				let arr = this.students.filter(item => {
					if(this.updateStu.id==item.id){
						item.id = this.updateStu.id;
						item.name = this.updateStu.name;
						item.address = this.updateStu.address
						return item;
					}
				})
				this.students = arr
				this.updateStu={}
				this.dialogFormVisible = false
			},
			cancelUpdate(){
				this.updateStu={}
				this.dialogFormVisible=false
				this.updateStu={}
				
			}
			
		}
	}
</script>

<style>
</style>